<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\cache\driver\Redis;

class Auth extends Controller
{
    public function __construct(Request $request)
    {
        parent::__construct($request);
        //获取相关的配置的信息
//        $this->assign(getConfig());
        $admin_id = session('userid');
        $admin = Db::name('admin')->alias('a')->join('nt_admin_info b', 'a.id=b.admin_id', 'left')->field('a.*,b.firstname,b.lastname,b.birthday,b.remarks,b.sex,b.marry,b.wage,b.education,b.username')->where('a.id', $admin_id)->find();
        if ($admin) {
            session('admin', $admin);//存储的是用户的信息
        } else {
            $this->redirect('admin/login/login');
        }
        if (isset($this->data['lang_type']) && $this->data['lang_type']) {
            $this->lang = $this->data['lang_type'];
        } else {
            $this->lang = is_lang();
        }
        //$this->redis = new Redis();
        //检测权限
        $this->checkauth();
        //菜单权限
        $this->getauth();
    }

    //获取当前登录用户的菜单权限
    protected function getauth()
    {
        //获取当前用户登录的角色id
        $admin = session('admin');
        $position_id = $admin['position_id'];
        if ($position_id == 1) {
            //是否展示 是否停用 菜单权限
            $field = ['zu.*,mu.content'];
            $menu = getTreeDatas('menu', [['multi_lang mu', 'zu.mkey = mu.mkey']], $field, ['zu.is_menu' => 1, 'zu.disable' => 0, 'mu.lang' => $this->lang]);
        } else {
            //非管理员
            $manage_auth = $admin['auth_ids'];
            $field = ['zu.*,mu.content'];
            $menu = getTreeDatas('menu', [['multi_lang mu', 'zu.mkey = mu.mkey']], $field, ['zu.is_menu' => 1, 'zu.disable' => 0, 'mu.lang' => $this->lang, 'zu.id' => ['in', $manage_auth]]);
        }
        //设置菜单栏的显示
        $this->assign('menu', $menu);
    }

    //防止用户手动改变url地址
    protected function checkauth()
    {
        $admin = session('admin');
        //获取当前用户的角色id
        $position_id = $admin['position_id'];
        //管理员不需要检测
        if ($position_id == 1) {
            return;
        }
        //获取控制器与方法
        $controller = request()->controller();
        $action = request()->action();
        if ($controller == 'Index' && $action == 'index') {
            //首页不要检测
            return;
        }
        //获取这个用户的个人权限id
        $auth_manage = explode(',', $admin['auth_ids']);
        //当前页面，对应的权限
        $auth =Db::name('menu')->where([
            'control' => strtolower($controller),
            'option' => strtolower($action),
            'disable' => 0//是否停用
        ])->find();
        //判断用户的个人权限did
        if (!in_array($auth['id'], $auth_manage)) {
            if (request()->isAjax() && request()->isPost()) {
                //表示没有权限
                echo json_encode(array('msg'=>lang("no_permission,_please_contact_administrator"),'status'=>3,));
                exit;
            } elseif (request()->isGet()) {
                //表示没有权限
                echo lang("no_permission,_please_contact_administrator");
                exit;
            }else{
                echo lang("no_permission,_please_contact_administrator");
                exit;
            }
        }
        return;
    }

    // 获取当前语言类型
    public function getLang($lang_type = '')
    {
        if ($lang_type && $lang_type) {
            return is_lang($lang_type);
        } else {
            return is_lang();
        }
    }


    // 获取redis 中的数据
    public function getRedisData($keyName)
    {
        if ($this->redis->get($keyName)) {
            return $this->redis->get($keyName);
        } else {
            return '';
        }
    }


    // 设置redis 数据
    public function setRedisData($keyName, $value)
    {
        if ($this->redis->set($keyName, $value)) {
            return true;
        } else {
            return false;
        }
    }

}